library(lme4)
library(texreg)
library(stargazer)

setwd("code/")
source("insurance_source_score_1_prep.R")
setwd("code/")
source("insurance_source_score_3_functions.R")

##### load data ----
## load("/Users/wrh75/Dropbox/ACAattitudes/data/survey/paneldata/merged-2020-n2471-03152020.Rdata")
load(file="data/table2-panel-aca-replication-12112020.Rdata")

## ##### recode data ----

## #####
dta21$SELFINSURE10 <- NA
dta21$SELFINSURE10[dta21$DH8_10=="Plan you purchased yourself"] <- 1
dta21$SELFINSURE10[! dta21$DH8_10=="Plan you purchased yourself" &
                     ! dta21$DH8_10 %in% c(NA)] <- 0
dta21$SELFINSURE10[dta21$DH7_10=="Not covered by health insurance"] <- 0

dta21$SELFINSURE11 <- NA
dta21$SELFINSURE11[dta21$DH8_11=="Plan you purchased yourself"] <- 1
dta21$SELFINSURE11[! dta21$DH8_11=="Plan you purchased yourself" &
                     ! dta21$DH8_11 %in% c(NA)] <- 0
dta21$SELFINSURE11[dta21$DH7_11=="Not covered by health insurance"] <- 0

dta21$SELFINSURE13 <- NA
dta21$SELFINSURE13[dta21$DH8=="Plan you purchased yourself"] <- 1
dta21$SELFINSURE13[! dta21$DH8=="Plan you purchased yourself" &
                     ! dta21$DH8 %in% c(NA)] <- 0
dta21$SELFINSURE13[dta21$DH7=="Not covered by health insurance"] <- 0

dta21$SELFINSURE14 <- NA
dta21$SELFINSURE14[dta21$DH8_14=="Plan you purchased yourself"] <- 1
dta21$SELFINSURE14[! dta21$DH8_14=="Plan you purchased yourself" &
                     ! dta21$DH8_14 %in% c(NA)] <- 0
dta21$SELFINSURE14[dta21$DH7_14=="Not covered by health insurance"] <- 0

dta21$UNINSURED10 <- NA
dta21$UNINSURED10[dta21$DH7_10=="Not covered by health insurance"] <- 1
dta21$UNINSURED10[! dta21$DH7_10=="Not covered by health insurance" &
                    ! dta21$DH7_10 %in% c(NA)] <- 0

dta21$UNINSURED11 <- NA
dta21$UNINSURED11[dta21$DH7_11=="Not covered by health insurance"] <- 1
dta21$UNINSURED11[! dta21$DH7_11=="Not covered by health insurance" &
                    ! dta21$DH7_11 %in% c(NA)] <- 0

dta21$UNINSURED13 <- NA
dta21$UNINSURED13[dta21$DH7=="Not covered by health insurance"] <- 1
dta21$UNINSURED13[! dta21$DH7=="Not covered by health insurance" &
                     ! dta21$DH7 %in% c(NA)] <- 0

dta21$UNINSURED14 <- NA
dta21$UNINSURED14[dta21$DH7_14=="Not covered by health insurance"] <- 1
dta21$UNINSURED14[! dta21$DH7_14=="Not covered by health insurance" &
                    ! dta21$DH7_14 %in% c(NA)] <- 0


dta21$intercept <- rep(1, nrow(dta21))
dta21$BLACK <- as.integer(dta21$xethm_12=="Black, Non-Hispanic")
dta21$HISP <- as.integer(dta21$xethm_12=="Hispanic")
dta21$ASIAN <- 0
dta21$MALE <- as.integer(dta21$PPGENDER=="Male")
dta21$age <- as.numeric(as.character(dta21$ppage_14))
##
## 6 = ``Less than high school (Grades 1-8 or no formal schooling)"\\
## 10 = ``High school incomplete (Grades 9-11 or Grade 12 with no diploma)"\\
## 12 = ``High school graduate (Grade 12 with diploma or GED certificate)"\\
## 13 = ``Some college, no degree (includes some community college)"\\
## 14 = ``Two year associate degree from a college or university"\\
## 16 = ``Four year college or university degree/Bachelor's degree (e.g., BS, BA, AB)"\\
## 17 = ``Some postgraduate or professional school, no postgraduate degree"\\
## 19 = ``Post-graduate or professional degree, including master's, doctorate, medical, or law degree (e.g., MA, MS, PhD, MD, JD)"\\
dta21$educ <- dplyr::recode(
    dta21$PPEDUC,
    "No formal education" = "6",
    "1st, 2nd, 3rd, or 4th grade" = "6",
    "5th or 6th grade" = "6",
    "7th or 8th grade" = "6",
    "9th grade" = "10",
    "10th grade" = "10",
    "11th grade" = "10",
    "12th grade NO DIPLOMA" = "10",
    "HIGH SCHOOL GRADUATE - high school DIPLOMA or the equivalent (GED)" = "12",
    "Some college, no degree" = "13",
    "Associate degree" = "14",
    "Bachelors degree" = "16",
    "Masters degree" = "18",
    "Professional or Doctorate degree" = "19"
    )
dta21$educ <- as.numeric(as.character(dta21$educ))
dta21$income <- dplyr::recode(
    dta21$PPINCIMP,
    "Less than $5,000" = "2.5",
    "$7,500 to $9,999" = "8.75",
    "$10,000 to $12,499" = "11.25",
    "$12,500 to $14,999" = "13.75",
    "$15,000 to $19,999" = "17.5",
    "$20,000 to $24,999" = "22.5",
    "$25,000 to $29,999" = "27.5",
    "$30,000 to $34,999" = "32.5",
    "$35,000 to $39,999" = "37.5",
    "$40,000 to $49,999" = "45",
    "$50,000 to $59,999" = "55",
    "$60,000 to $74,999" = "67.5",
    "$75,000 to $84,999" = "80",
    "$85,000 to $99,999" = "92.5",
    "$100,000 to $124,999" = "112.5",
    "$125,000 to $149,999" = "137.5",
    "$150,000 to $174,999" = "162.5",
    "$175,000 to $199,999" = "187.5",
    "$200,000 to $249,999" = "225",
    "$250,000 or more" = "250"
    )
dta21$income <- as.numeric(as.character(dta21$income))

## ## note: variables used in main analysis are from 2012/pre-implementation - above variables are post-implmementation, as in KFF

lms_uninsured <- plot_slopes_return_lmer_list(
    outcome="FAVOR>=3",# irrelevant here
    propensity="COVERED == 0",
    the_subset=subset(
        sdat.under65.2,
        INCOME > 40
    ),
    plot_title="",
    pdf_name="scratch.pdf",
    no_pdf = TRUE,
    no_polynomials=TRUE, color="black"
)
coefs <- coef(lms_uninsured[["propensity_out_lm"]])

lms_self <- plot_slopes_return_lmer_list(
    outcome="FAVOR>=3",# irrelevant here
    propensity="SELFINSURE == 1",
    the_subset=subset(
        sdat.under65.2,
        INCOME > 40
    ),
    plot_title="",
    pdf_name="scratch.pdf",
    no_pdf = TRUE,
    no_polynomials=TRUE, color="black"
)
coefs_self <- coef(lms_self[["propensity_out_lm"]])

dta21$pscore <- as.matrix(dta21[,c(
    "intercept",
    "educ",
    "income",
    "age",
    "BLACK","HISP","ASIAN",
    "MALE","RETIRED10"
)]) %*% coefs

dta21$pscore_self <- as.matrix(dta21[,c(
    "intercept",
    "educ",
    "income",
    "age",
    "BLACK","HISP","ASIAN",
    "MALE","RETIRED10"
)]) %*% coefs_self

age_cutoff <- 65



mod1314_become_uninsured <- (
    lm(
        UNINSURED13==1 | UNINSURED14==1 ~ pscore,
        data = subset(
            dta21,
            (UNINSURED10==0  | UNINSURED11==0)
            & income > 40
            & age < age_cutoff
        ),
        weights=weight1_11
    )
)

mod1314_uninsured <- (
    lm(
        UNINSURED13==1 | UNINSURED14==1 ~ pscore,
        data = subset(
            dta21,
            income > 40
            & age < age_cutoff
        ),
        weights=weight1_11
    )
)


stargazer(
    mod1314_uninsured,
    mod1314_become_uninsured,
    type="latex",
    star.cutoffs = c(0.05, 0.01, 0.001),
    digits=2,
    column.labels=c("Uninsured 2018-2020", "Insured 2016, Become Uninsured 2018-2020"),
    out=paste0(
        the_prefix, "/figs/",
        "tableA22_insurance_score_uninsured_panel_comparison_models.tex"
    )
)


mod1314_become_selfinsured <- (
    lm(
        SELFINSURE14 == 1 | SELFINSURE13==1 ~ pscore_self,
        data = subset(
            dta21,
            (SELFINSURE10==0 | SELFINSURE11==0)
            & income > 40
            & age < age_cutoff
        ),
        weights=weight1_11
    )
)

mod1314_selfinsured <- (
    lm(
        SELFINSURE14==1 | SELFINSURE13==1 ~ pscore_self,
        data = subset(
            dta21,
            income > 40
            & age < age_cutoff
        ),
        weights=weight1_11
    )
)


stargazer(
    mod1314_selfinsured,
    mod1314_become_selfinsured,
    type="latex",
    star.cutoffs = c(0.05, 0.01, 0.001),
    digits=2,
    column.labels=c("Self-insured 2018-2020", "Not self-insured 2016, Become Self-Insured 2018-2020"),
    out=paste0(
        the_prefix, "/figs/",
        "tableA25_insurance_score_selfinsured_panel_comparison_models.tex"
    )
)
